package engine.net.utils
{
	import flash.utils.ByteArray;
	
	import newx.utils.debug.IO;
	
	
	public class UnicodeCovert
	{
		
		//Unicode编码(16进制)　║　UTF-8 字节流(二进制) 
		//000000 - 00007F　║　0xxxxxxx 
		//000080 - 0007FF　║　110xxxxx 10xxxxxx 
		//000800 - 00FFFF　║　1110xxxx 10xxxxxx 10xxxxxx 
		//010000 - 10FFFF　║　11110xxx 10xxxxxx 10xxxxxx 10xxxxxx 
		
		public static function ConvertStr(char:String):ByteArray
		{
			var utf8:ByteArray = new ByteArray();
			utf8.writeMultiByte(char, "utf-8");
			utf8.position = 0;
			return Convert(utf8);
		}
		
		public static function Convert(utf8:ByteArray):ByteArray
		{
			var L:int = 0;
			var H:int = 0;
			var C1:int = 0;
			var C2:int = 0;
			var C3:int = 0;
			var C4:int = 0;
			var code16:uint = 0;
			var retUnicode:ByteArray = new ByteArray();
			if (utf8.length == 1) 
			{
				L = utf8.readByte() & 0x7F;
			}
			else if (utf8.length == 2)
			{
				C2 = utf8.readByte();
				C1 = utf8.readByte();
				code16 = (C2 & 0x1F) << 6;
				code16 += (C1 & 0x3F);
				L = code16 & 0xFF;
				H = (code16 >> 8) & 0xFF;
			}
			else if (utf8.length == 3)
			{
				C3 = utf8.readByte() & 0xFF;
				C2 = utf8.readByte() & 0xFF;
				C1 = utf8.readByte() & 0xFF;
				code16 = ((C3 & 0xF) << 12);
				code16 += ((C2 & 0x3F) << 6);
				code16 += (C1 & 0x3F);
				L = code16 & 0xFF;
				H = (code16 >> 8) & 0xFF;
			}
			else if (utf8.length == 4)
			{
				C4 = utf8.readByte();
				C3 = utf8.readByte();
				C2 = utf8.readByte();
				C1 = utf8.readByte();
				code16 += (C4 & 0x07) << 18;
				code16 += (C3 & 0x3F) << 12;
				code16 += (C2 & 0x3F) << 6;
				code16 += (C1 & 0x3F);
				L = code16 & 0xFF;
				H = (code16 >> 8) & 0xFF;
			}
			else
			{
				IO.traceLog("utf-8 len error");
			}
			retUnicode.writeByte(L & 0xFF);
			retUnicode.writeByte(H & 0xFF);
			return retUnicode;
			
		}
		
		public static function Convert8(unicode:ByteArray):ByteArray
		{
			var low:int = unicode.readByte() & 0xFF;
			var high:int = unicode.readByte() & 0xFF;		
			var ret8:ByteArray = new ByteArray();		
			var code16:int = (high<<8) + low;

			var C1:int = 0;
			var C2:int = 0;
			var C3:int = 0;
			var C4:int = 0;
			if ((code16) <= 0x7f)
			{
				//000000 - 00007F　║　0xxxxxxx 
				ret8.writeByte(low);
				ret8.position = 0;
				return ret8;
			}
			else if ((code16) <= 0x7FF)
			{
				//000080 - 0007FF　║　110xxxxx 10xxxxxx 
				C1 = 0x80 + (code16 & 0x3F);
				C2 = 0xC0 + ((code16 >> 6) & 0x1F);
				ret8.writeByte(C2);
				ret8.writeByte(C1);
				ret8.position = 0;
				return ret8;
			}
			else if ((code16) <= 0xFFFF)
			{
				//000800 - 00FFFF　║　1110xxxx 10xxxxxx 10xxxxxx 
				C1 = 0x80 + (code16 & 0x3F);
				C2 = 0x80 + ((code16 >> 6) & 0x3F);
				C3 = 0xE0 + ((code16 >> 12) & 0xF);
				ret8.writeByte(C3);
				ret8.writeByte(C2);
				ret8.writeByte(C1);
				ret8.position = 0;
				return ret8;
			}
			else
			{
				//010000 - 10FFFF　║　11110xxx 10xxxxxx 10xxxxxx 10xxxxxx 
				C1 = 0x80 + (code16 & 0x3F);
				C2 = 0x80 + ((code16 >> 6) & 0x3F);
				C3 = 0x80 + ((code16 >> 12) & 0x3F);
				C4 = 0xF0 + ((code16 >> 18) & 0x07);
				ret8.writeByte(C4);
				ret8.writeByte(C3);
				ret8.writeByte(C2);
				ret8.writeByte(C1);
				ret8.position = 0;
				return ret8;
			}	
		}
	}
}